from django.core.management.base import BaseCommand
from faker import Faker
from backend.cews.models import StockItem
from backend.cews.services.PartsService import save_part
from backend.cews.tests.factories import PartsFactory, StockItemEDocFactory, StockItemFactory, StockItemRelationFactory, StockItemReplacementFactory, StockItemTaskFactory
class Command(BaseCommand):
    help = 'Create test data'
 
            # 'serial_no':stock_item.serial_no,
            # 'short_description':stock_item.short_description,
            # 'equipment_class':stock_item.equipment_class,
            # 'parent_part_stock_code':stock_item.parent_part_stock_code,
            # 'parent_part_serial_number':stock_item.parent_part_serial_number,
    
    def handle(self, *args, **options):
        
        faker:Faker
        faker = Faker()    
        
        stocks = []
        parts = []
        
        stocks.append(StockItemFactory(lv5=''))
        stocks.append(StockItemFactory())
        stocks.append(StockItemFactory())
        
        ## stock item edoc
        StockItemEDocFactory(stock_item=stocks[0],stock_code=stocks[0].stock_code)
        StockItemEDocFactory(stock_item=stocks[0],stock_code=stocks[0].stock_code)
        StockItemEDocFactory(stock_item=stocks[0],stock_code=stocks[0].stock_code)
        StockItemEDocFactory(stock_item=stocks[1],stock_code=stocks[1].stock_code)
        StockItemEDocFactory(stock_item=stocks[1],stock_code=stocks[1].stock_code)
        StockItemEDocFactory(stock_item=stocks[2],stock_code=stocks[2].stock_code)

        ## Stock item task
        StockItemTaskFactory(stock_item=stocks[0])
        StockItemTaskFactory(stock_item=stocks[0])
        StockItemTaskFactory(stock_item=stocks[1])
        StockItemTaskFactory(stock_item=stocks[2])
        
        # relation
        StockItemRelationFactory(parent=stocks[0],child=stocks[1])
        StockItemRelationFactory(parent=stocks[0],child=stocks[2])
        
        ## hard time replacement
        sir1 = StockItemReplacementFactory(stock_item=stocks[0])
        sir2 = StockItemReplacementFactory(stock_item=stocks[1])
        sir3 = StockItemReplacementFactory(stock_item=stocks[2])
        
        p1,_ = save_part(
            serial_no=faker.ean8(),
            short_description='PARENT A',
            equipment_class='',
            parent_part_serial_number='',
            parent_part_stock_code='',
            **stocks[0].parts_info,
            car='car',workgroup='wg')   
        
        p2,_ = save_part(
            serial_no=faker.ean8(),
            short_description='CHILD A',
            equipment_class='',
            parent_part_serial_number=p1.serial_no,
            parent_part_stock_code=p1.stock_code,
            **stocks[1].parts_info,
            car='car',workgroup='wg')
        
        p3,_ = save_part(
            serial_no=faker.ean8(),
            short_description='CHILD B',
            equipment_class='',
            parent_part_serial_number=p1.serial_no,
            parent_part_stock_code=p1.stock_code,
            **stocks[2].parts_info,
            car='car',workgroup='wg')   
        
        parts.append(p1)
        parts.append(p2)
        parts.append(p3)

        # parts.append(PartsFactory(stock_code=stocks[0].stock_code))
        # parts.append(PartsFactory(stock_code=stocks[1].stock_code))
        # parts.append(PartsFactory(stock_code=stocks[2].stock_code))
        
      